//
//  NavServices.m
//  YiCuiMiaoZhang
//
//  Created by Sings on 2019/3/29.
//  Copyright © 2018 益萃网络科技（中国）有限公司. All rights reserved.
//

#import "NavServices.h"
#import "UIWindow+BZ.h"
#import "BZAnimation.h"

#define isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

@interface NavServices ()

@property (nonatomic, strong) UIWindow *window;

@end

@implementation NavServices

- (UIWindow *)window {
    if ([BaseRegisterModel sharedInstance].window == nil) {
        _window = [UIWindow new];
    }else {
        _window = [BaseRegisterModel sharedInstance].window;
    }
    return _window;
}

- (void)pushViewModel:(BaseControllerViewModel *)viewModel animated:(BOOL)animated {

    if (viewModel == nil) { return; }
    [[self.window windowsLastViewController].navigationController
        pushViewController:[[MZRouting sharedInstance] getViewControllerWithViewModel:viewModel]
                  animated:animated];
}

- (void)pushMultiViewsWithModel:(MultiControllersViewModel *)viewModel animated:(BOOL)animated {
    if (viewModel && [viewModel isKindOfClass:[MultiControllersViewModel class]]) {

        UIViewController *rootViewController = [[MZRouting sharedInstance] getViewControllerWithViewModel:viewModel];
        
        UINavigationController *nav = [self.window windowsLastViewController].navigationController;

        NSArray<BaseControllerViewModel *> *arrUpperLayerViewModels = [viewModel upperLayerControllerViewModels];
        if (arrUpperLayerViewModels && arrUpperLayerViewModels.count > 0) {
            NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:nav.viewControllers];
            [viewControllers addObject:rootViewController];
            for (BaseControllerViewModel *upperViewModel in arrUpperLayerViewModels) {
                UIViewController *upperViewController = [[MZRouting sharedInstance] getViewControllerWithViewModel:upperViewModel];
                if (upperViewController) {
                    [viewControllers addObject:upperViewController];
                }
            }
            [nav setViewControllers:viewControllers animated:animated];
        } else {
            [nav pushViewController:rootViewController animated:animated];
        }
    }
}

- (void)popViewModelAnimated:(BOOL)animated {
    [[self.window windowsLastViewController].navigationController
        popViewControllerAnimated:animated];
}

- (void)popToRootViewModelAnimated:(BOOL)animated {
    [[self.window windowsLastViewController].navigationController
        popToRootViewControllerAnimated:animated];
}

- (void)presentViewModel:(BaseControllerViewModel *)viewModel
                animated:(BOOL)animated
              completion:(void (^)(void))completion {
    
    if ([[self.window windowsLastViewController] respondsToSelector:@selector(presentAnimation)]) {
        [[self.window windowsLastViewController] presentAnimation];
    }
    BZBaseViewController *pushVC = [[MZRouting sharedInstance] getViewControllerWithViewModel:viewModel];
    pushVC.modalPresentationStyle = UIModalPresentationFullScreen;
    [[self.window windowsLastViewController]
        presentViewController:pushVC
                     animated:animated
                   completion:completion];
}

- (void)dismissViewModelAnimated:(BOOL)animated completion:(void (^)(void))completion {
    [[self.window windowsLastViewController] dismissAnimation];
    [[self.window windowsLastViewController] dismissViewControllerAnimated:animated
                                                                completion:completion];
}

- (void)resetRootViewModel:(BaseControllerViewModel *)viewModel {
    [BZAnimation setRootVC:[[MZRouting sharedInstance] getViewControllerWithViewModel:viewModel]];
}

- (void)popToViewModel:(BaseControllerViewModel *)viewMdoel
          ForQueryType:(NavigationPopQueryType)queryType
              Animated:(BOOL)animated
               Failure:(void (^)(void))failure {
    NSArray *vcArray = [self.window windowsLastViewController].navigationController.viewControllers;
    BZBaseViewController *popVC;
    switch (queryType) {
        case NavigationPopQueryTypeFirst:
            for (int i = 0; i < vcArray.count; i++) {
                popVC = [self compareShowViewModel:viewMdoel AndNowViewController:vcArray[i]];
                if (popVC != nil) { break; }
            }
            break;
        case NavigationPopQueryTypeLast:
            for (int i = vcArray.count - 1; i >= 0; i--) {
                popVC = [self compareShowViewModel:viewMdoel AndNowViewController:vcArray[i]];
                if (popVC != nil) { break; }
            }
            break;
    }

    if (popVC != nil) {
        [[self.window windowsLastViewController].navigationController popToViewController:popVC
                                                                                 animated:animated];
    } else {
        if (failure) { failure(); }
    }
}

- (BZBaseViewController *)compareShowViewModel:(BaseControllerViewModel *)showViewModel
                          AndNowViewController:(BZBaseViewController *)nowVC {

    Class showC = [[MZRouting sharedInstance] getViewControllerClassWithViewModel:showViewModel];

    if ([nowVC isMemberOfClass:showC]) { return nowVC; }
    return nil;
}

- (void) presentiOSActivityControllerWihtActivityItems:(NSArray *) items AndPadctivityView:(UIView *) vPadActivity {
    UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
    if (isPad){
        activityViewController.popoverPresentationController.barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:vPadActivity];
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            [[self.window windowsLastViewController] presentViewController:activityViewController animated:YES completion:nil];
        }];
    }else{
        [[self.window windowsLastViewController] presentViewController:activityViewController animated:YES completion:nil];
    }
}

- (void)presentViewModelWithPushAnimation:(BaseControllerViewModel *)viewModel animated:(BOOL)animated completion:(void (^)(void))completion {
    
    CATransition *animation = [CATransition animation];
    animation.duration = 0.3;
    animation.timingFunction = UIViewAnimationCurveEaseInOut;
    animation.type = kCATransitionPush;
    animation.subtype = kCATransitionFromRight;
    
    UIViewController *vc = [self.window windowsLastViewController];
    vc.view.window.layer.backgroundColor = BZTHEME_COLOR(skin_content_bg_color).CGColor;
    vc.view.window.backgroundColor = BZTHEME_COLOR(skin_content_bg_color);
    [vc.view.window.layer addAnimation:animation forKey:nil];
    [vc presentViewController:[[MZRouting sharedInstance] getViewControllerWithViewModel:viewModel] animated:animated completion:completion];
    
}


@end
